//
//  CTUserSubjectsList.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/25.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTUserSubjectsList.h"
#import "CTUserSubjectCell.h"
#import "CTHeader.h"
#import "CTDeviceFunction.h"
#import "CTDebugManager.h"
#import "CTSegmentView.h"
#import "RefreshControl.h"


@interface CTUserSubjectsList()<UICollectionViewDataSource,UICollectionViewDelegate,RefreshControlDelegate>
{
    UICollectionView *userFeaturesCollectionView;
    UICollectionViewFlowLayout *flowLayout;
    CGSize size;
    UIView * collectionHeaderView;
    
    RefreshControl * refreshControl;
}

@end
@implementation CTUserSubjectsList

@synthesize userSubArray,delegate;

- (void)dealloc
{
    collectionHeaderView = nil;
    userFeaturesCollectionView = nil;
    
    [userFeaturesCollectionView removeFromSuperview];
    userFeaturesCollectionView = nil;
    userSubArray = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTUserPublishCollectionListView Is Removed!!!"];
    
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self createUserCollectionView];
        DeviceTypeAdaptation adapation = [CTDeviceFunction deviceTypeForResolutionAdaptation];
        switch (adapation) {
            case iPhone4_4s:
                size = CGSizeMake((CT_HEADER_SCREEN_WIDTH-50)/2, (CT_HEADER_SCREEN_WIDTH)/2);
                break;
            case iPhone5_5s_5c:
                size = CGSizeMake((CT_HEADER_SCREEN_WIDTH-55)/2, (CT_HEADER_SCREEN_WIDTH)/2);
                break;
            case iPhone6:
                size = CGSizeMake((CT_HEADER_SCREEN_WIDTH-65)/2, (CT_HEADER_SCREEN_WIDTH-50)/2);
                break;
            case iPhone6_Plus:
                size = CGSizeMake((CT_HEADER_SCREEN_WIDTH-75)/2, (CT_HEADER_SCREEN_WIDTH-70)/2);
                break;
            default:
                break;
        }
    }
    return self;
}
//创建CollectionView
- (void)createUserCollectionView
{

    
    flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.headerReferenceSize = CGSizeMake(CT_HEADER_SCREEN_WIDTH, 0);//头部
    userFeaturesCollectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout];
    userFeaturesCollectionView.delegate = self;
    userFeaturesCollectionView.dataSource = self;
    [self addSubview:userFeaturesCollectionView];
    userFeaturesCollectionView.backgroundColor = [UIColor clearColor];
    [userFeaturesCollectionView registerClass:[CTUserSubjectCell class] forCellWithReuseIdentifier:@"cell"];
    [userFeaturesCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView"];
    
    /*
     *** refresh control
     */
    refreshControl = [[RefreshControl alloc] initWithScrollView:userFeaturesCollectionView delegate:self];
    refreshControl.topEnabled = YES;
    refreshControl.bottomEnabled = NO;
    refreshControl.enableInsetBottom = 60;
}
#pragma mark - setter -
- (void)setUserPublishArr:(NSArray *) ary
{
    userSubArray = [NSArray arrayWithArray:ary];
    
    
    if (userSubArray.count >= 10) {
        refreshControl.bottomEnabled = YES;
    }else{
        refreshControl.bottomEnabled = NO;
    }
//    int countHeight = 0;
//    if (userSubArray.count%2 == 0)
//    {
//        countHeight =(int)userSubArray.count/2;
//        userFeaturesCollectionView.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, countHeight*size.height+countHeight*20+20);
//    }
//    else
//    {
//        countHeight = (int)userSubArray.count/2+1;
//        userFeaturesCollectionView.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, countHeight*size.height+countHeight*20+20);
//    }
    [userFeaturesCollectionView reloadData];
    
}

#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [userSubArray count];
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identify = @"cell";
    
    CTUserSubjectCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    [cell sizeToFit];
    
    //    CTSubjectTimeLineItem * itm = (CTSubjectTimeLineItem *)[subjectArr objectAtIndex:indexPath.row];
    //    cell.imgUrl = itm.thumb;
    //    cell.name = itm.name;
    //    cell.describe = itm.tag_describe;
    //    if ([itm.tag_trends isEqualToString:@"0"])cell.isTrends = NO;
    //    else cell.isTrends = YES;
    //    if ([itm.tag_official isEqualToString:@"0"])cell.isOfficial = NO;
    //    else cell.isOfficial = YES;
    
    return cell;
}

//头部显示的内容
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                                            UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView" forIndexPath:indexPath];
    
    [headerView addSubview:collectionHeaderView];//头部广告栏
    return headerView;
}

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return size;
    
}
//定义每个UICollectionView 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10,10,0,10);
}
//定义UICollectionView 每个item的纵向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

//定义每个UICollectionView 纵向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 3;
}

#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //CTSubjectTimeLineItem * itm = (CTSubjectTimeLineItem *)[subjectArr objectAtIndex:indexPath.row];
    if ([self.delegate respondsToSelector:@selector(userSubjectPublishCollectionListViewClicked:)])
    {
        [self.delegate userSubjectPublishCollectionListViewClicked:nil];
    }
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


#pragma mark - refresh control delegate -

- (void)refreshControl:(RefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection) direction
{
    if (direction == RefreshDirectionTop) {
        if ([self.delegate respondsToSelector:@selector(refreshSubjectCollectionControlPullDown:)]) {
            [self.delegate refreshSubjectCollectionControlPullDown:self];
        }
        
    }else{
        if ([self.delegate respondsToSelector:@selector(refreshSubjectCollectionControlPullUp:)]) {
            [self.delegate refreshSubjectCollectionControlPullUp:self];
        }
    }
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([self.delegate respondsToSelector:@selector(userSubjectPublishCollectionListScrollViewDidScroll:)]) {
        [self.delegate userSubjectPublishCollectionListScrollViewDidScroll:scrollView];
    }
    
    if (scrollView.contentOffset.y >= 0.0 && scrollView.contentOffset.y < 80) {
        [refreshControl finishRefreshingDirection:RefreshDirectionTop];
    }
    if (scrollView.contentOffset.y + scrollView.frame.size.height<= scrollView.contentSize.height && scrollView.contentOffset.y + scrollView.frame.size.height >= scrollView.contentSize.height - 80) {
        [refreshControl finishRefreshingDirection:RefreshDirectionBottom];
    }
    
    if (scrollView.contentOffset.y + scrollView.frame.size.height <= scrollView.contentSize.height+50) {
        refreshControl.isPullDown = YES;
    }else{
        refreshControl.isPullDown = NO;
    }
}






#pragma mark - action -

-(void)refresh
{
    [refreshControl startRefreshingDirection:RefreshDirectionTop];
}

-(void)refreshPullDownEnd
{
    [refreshControl finishRefreshingDirection:RefreshDirectionTop];
}

-(void)refreshPullUpEnd
{
    [refreshControl finishRefreshingDirection:RefreshDirectionBottom];
}

-(void)reloadData
{
    [userFeaturesCollectionView reloadData];
}


#pragma mark - action -
-(void)setHeadView:(UIView *)headView
{
    
    CTSegmentView * segView = (CTSegmentView *)[headView viewWithTag:10000];
    
    NSArray * subViewsArray = [segView subviews];
    for (UIButton * btn in subViewsArray) {
        
        if (btn.tag == 1) {
            [btn setBackgroundImage:[UIImage imageNamed:@"liebiao"] forState:UIControlStateNormal];
        }else if (btn.tag == 2)
        {
            [btn setBackgroundImage:[UIImage imageNamed:@"gongge"] forState:UIControlStateNormal];
        }else if (btn.tag == 4)
        {
            [btn setBackgroundImage:[UIImage imageNamed:@"zhuanti"] forState:UIControlStateNormal];
        }
        
    }
    
    
    
    
    UIButton * btn = (UIButton *)[segView viewWithTag:4];
    [btn setBackgroundImage:[UIImage imageNamed:@"zhuanti_click"] forState:UIControlStateNormal];
    
    UIImageView * segLineView = (UIImageView *)[segView viewWithTag:1000];
    
    
    CGRect rect = segLineView.frame;
    rect.origin.x = btn.frame.origin.x -(segLineView.frame.size.width - btn.frame.size.width)/2.0;
    segLineView.frame = rect;

    
    flowLayout.headerReferenceSize = CGSizeMake(CT_HEADER_SCREEN_WIDTH, headView.bounds.size.height);//头部
    
    collectionHeaderView = nil;
    collectionHeaderView = headView;
    [userFeaturesCollectionView reloadData];
}


@end
